#include "spi_sew.h"

/*SPI hardware configuration 
SPI1_NSS	PA4
SPI1_SCK  PA5
SPI1_MISO	PA6
SPI1_MOSI	PA7

*/

void RCC_Configuration(void)
{
  /* PCLK2 = HCLK/2 */
  RCC_PCLK2Config(RCC_HCLK_Div2); 

  /* Enable peripheral clocks --------------------------------------------------*/
  /* Enable SPI_MASTER clock and GPIO clock for SPI_MASTER and SPI_SLAVE */
  RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK | SPI_MASTER_CLK, ENABLE);	//SPI_SLAVE_GPIO_CLK |

//	Brauchen wir falls unser Mikro als SPI Slave fungieren soll
//  /* Enable SPI_SLAVE Periph clock */
//  RCC_APB1PeriphClockCmd(SPI_SLAVE_CLK, ENABLE);
}


void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure SPI_MASTER pins: SCK and MOSI ---------------------------------*/
  /* Configure SCK and MOSI pins as Alternate Function Push Pull */
  GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_SCK | SPI_MASTER_PIN_MOSI;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);
 
//	Brauchen wir falls unser Mikro als SPI Slave fungieren soll  
//  /* Configure SPI_SLAVE pins: SCK and MISO ---------------------------------*/
//  /* Configure SCK and MOSI pins as Input Floating */
//  GPIO_InitStructure.GPIO_Pin = SPI_SLAVE_PIN_SCK ;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//  GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure);
//  /* Configure MISO pin as Alternate Function Push Pull */
//  GPIO_InitStructure.GPIO_Pin = SPI_SLAVE_PIN_MISO;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//  GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure);
}


void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* 1 bit for pre-emption priority, 3 bits for subpriority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Configure and enable SPI_MASTER interrupt -------------------------------*/
  NVIC_InitStructure.NVIC_IRQChannel = SPI_MASTER_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

//	Brauchen wir falls unser Mikro als SPI Slave fungieren soll
//  /* Configure and enable SPI_SLAVE interrupt --------------------------------*/
//  NVIC_InitStructure.NVIC_IRQChannel = SPI_SLAVE_IRQn;
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
//  NVIC_Init(&NVIC_InitStructure);
}


TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
  while (BufferLength--)
  {
    if (*pBuffer1 != *pBuffer2)
    {
      return FAILED;
    }

    pBuffer1++;
    pBuffer2++;
  }

  return PASSED;
}

void SpiMasterConfiguration(void)
{
	SPI_InitTypeDef SPI_InitMaster;

	/* SPI_MASTER configuration ------------------------------------------------*/
  SPI_InitMaster.SPI_Direction = SPI_Direction_1Line_Tx;
  SPI_InitMaster.SPI_Mode = SPI_Mode_Master;
  SPI_InitMaster.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitMaster.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitMaster.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitMaster.SPI_NSS = SPI_NSS_Soft;
  SPI_InitMaster.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  SPI_InitMaster.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitMaster.SPI_CRCPolynomial = 7;

	//Create SPI Master 
  SPI_Init(SPI_MASTER, &SPI_InitMaster);
 }

 void SpiSlaveConfiguration(void)
 {
 	SPI_InitTypeDef SPI_InitSlave;

 	/* SPI_SLAVE configuration -------------------------------------------------*/
  SPI_InitSlave.SPI_Direction = SPI_Direction_1Line_Rx;
  SPI_InitSlave.SPI_Mode = SPI_Mode_Slave;

	//Create SPI Slave
  SPI_Init(SPI_SLAVE, &SPI_InitSlave);
}